use crate::config::config::MyConfig;
use crate::runner::init;
use std::env::current_dir;
use std::path::PathBuf;
use std::process;
use wang_utils::git::{git_add, git_commit, git_init};

mod config;
mod runner;

fn main() {
    // 获取参数
    let args = MyConfig::get();
    // 生成目录
    match init(&args) {
        Ok(_) => {
            println!("生成成功");
        }
        Err(e) => {
            println!("出错了: {e}");
            process::exit(1);
        }
    }
    if args.git {
        if let Err(e) = init_git_folder(&args) {
            eprintln!("初始化git仓库出错:{e:?}");
            process::exit(1);
        }
        println!("初始化git仓库成功");
    }
}

fn init_git_folder(args: &MyConfig) -> anyhow::Result<()> {
    let path = PathBuf::new();
    let dir = current_dir()?;
    let path = path.join(dir);
    let path = path.join(args.name.clone());
    // git init
    let repo = git_init(path)?;
    // git add .
    git_add(&repo)?;
    // git commit -m "initial commit"
    git_commit(&repo, "initial commit")?;
    Ok(())
}
